package com.hxy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.common.JsonResult;
import com.hxy.dao.PaydetailMapper;
import com.hxy.dao.PayrecordMapper;
import com.hxy.dao.StuffinfoMapper;
import com.hxy.pojo.Paydetail;
import com.hxy.pojo.Stuffinfo;
import com.hxy.pojo.model.PayRecordSelectData;
import com.hxy.pojo.vo.PaydetailVo;
import com.hxy.pojo.vo.StuffinfoVo;
import com.hxy.service.PaydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class PaydetailServiceImpl  implements PaydetailService {
    @Autowired
    private PaydetailMapper paydetailMapper;
    @Autowired
    private StuffinfoMapper stuffinfoMapper;
    @Autowired
    private PayrecordMapper payrecordMapper;
    @Override
    public PaydetailVo queryById(Long id) {

        return paydetailMapper.selectByPrimaryKey(id);

    }

    @Override
    public PaydetailVo selectPaydetailVo(Long id) {
        PaydetailVo paydetailvo = paydetailMapper.selectByPrimaryKey(id);
        System.out.println(paydetailvo);
        StuffinfoVo stuffinfo = stuffinfoMapper.selectByPrimaryKey(paydetailvo.getEid());
        paydetailvo.setStuffname(stuffinfo.getStuffname());
        System.out.println(paydetailvo);
        return paydetailvo;
    }

    @Override
    public PageInfo<PaydetailVo> selectDetailPageInfoAll(PayRecordSelectData payRecordSectData) {
        PageHelper.startPage(payRecordSectData.getPage(),payRecordSectData.getLimit());
        List<PaydetailVo> paydetailVoList=paydetailMapper.selectDetailPageInfoAll(payRecordSectData);

        PageInfo<PaydetailVo> stuffinfoVoPageInfo=new PageInfo<>(paydetailVoList);
        return stuffinfoVoPageInfo;
    }

    @Override
    public JsonResult update(PaydetailVo paydetail) {
        int i=paydetailMapper.updateByVo(paydetail);
        PaydetailVo paydetailVo = paydetailMapper.selectByPrimaryKey(paydetail.getId());
        System.out.println(paydetail);
        BigDecimal money= paydetailVo.getAchieveUp().add(paydetailVo.getFoodUp()).add(paydetailVo.getLateDown()).add(paydetailVo.getLearlyDown()).add(paydetailVo.getOutUp()).add(paydetailVo.getOvertimeUp()).add(paydetailVo.getRunUp()).add(paydetailVo.getJobUp());
        int f=payrecordMapper.updateMoney(paydetail.getId(),money);
        if (i>0){
            return new JsonResult(0,"success",null);
        }
        else{
            return new JsonResult(1, "fail",null);
        }
    }
}
